// page p0002 controller
var p0002Controller = (function($) {

	var key = 'p0002';
	var selector = '#' + key;
	var page = 'pages/' + key + '.html';

	var fetch = function () {
		var key = $.esPage.queryParams.key;
		PlatformSupport.getItem(key, function (item) {
			if (!item) {
				alert('没有标签为' + key + '的密码信息');
				$.esPage.closeDialog();
			}

			draw(item);
		});
	};

	var draw = function (item) {
		if (!$.esPage.queryParams)
			return;

		$('#p0002IptKey').val(item.key);
		$('#p0002IptUsr').val(item.userName);
		$('#p0002IptPwd').val(item.password);
		$('#p0002IptRmk').val(item.remark);
	};

	var showDialog = function (callback, queryParams) {
		$.esPage.showDialog(selector, {
			callback: callback,
			queryParams: queryParams
		});
		app.afterLoad(selector);

		$('#p0002 input[type="text"]').val('');
		$('#p0002 textarea').val('');
		$('#p0002Title').text('记个新密码吧, 免得忘了');
		$('#p0002 input[type="text"], #p0002 textarea').prop('disabled', '');
		$('#p0002OKButton').show();
		if ($.esPage.queryParams.create)
			return;

		fetch();
		$('#p0002Title').text('改一改');
		$('#p0002IptKey').prop('disabled', 'disabled');
		if ($.esPage.queryParams.editable)
			return;

		$('#p0002Title').text('来瞅瞅');
		$('#p0002 input[type="text"], #p0002 textarea').prop('disabled', 'disabled');
		$('#p0002OKButton').hide();
	};

	var obj = {

		init: function (callback) {
			app.loadPage(page, function () {
				$(selector).esPage();
				$('#p0002IptRmk').autoHeight();
				if (callback)
					callback();
			});
		},

		refresh: draw,
		showDialog: showDialog,

		btnOKClick: function () {
			$.esPage.closeDialog(false, {
				key: $('#p0002IptKey').val(),
				userName: $('#p0002IptUsr').val(),
				password: $('#p0002IptPwd').val(),
				remark: $('#p0002IptRmk').val()
			});
		},

		btnCancelClick: function () {
			$.esPage.closeDialog(true);
		}
	};

	app.registerPageController(key, obj);

	return obj;

})(jQuery);